<?php
namespace Home\Controller;
class UserController extends HomeController {
    
    public function index(){
        $user = M('user');
        $data = $user->table('dota2_user_info ui, dota2_user u')->where('ui.ui_id=u.u_id and u.email="'.$_SESSION['email'].'"')->select();
        // dump($data);
        $iconpath = '/Uploads/person_icon/';
        $iconpath .= substr($data[0]['icon'],0,8).'/';
        $this->assign('iconpath',$iconpath);
        $this->assign('data',$data);
        // //交易记录
        $email = $_SESSION['email'];
        $uid = $_SESSION['uid'];
        $list = M()->table('dota2_orders')->join('dota2_goods')->join('dota2_o_detail')->where('o_uid="'.$uid.'" and o_d_gid = g_id and o_rand_id = o_d_oid and o_d_del = 0')->select();
        $this->assign('list',$list);
        //刀币 与 积分
        $this->assign('dotacoin',$data[0]['dotacoin']);
        $this->assign('score',$data[0]['score']);
        //我的收藏
        $list2 = M()->table('dota2_collection')->join('dota2_goods')->join('dota2_goodsimg')->where('coll_uid = "'.$uid.'" and coll_gid = g_id and g_id = gi_g_id and face = 1')->select();
        $this->assign('list2',$list2);
        $this->assign('uid',$_SESSION['uid']);

        //我的关注
        $passage = M('user_passage');
        $fans = $passage->where('uid ="'.$uid.'"')->count('up_id');
        $fanslist = M()->table('dota2_user_passage,dota2_user_info,dota2_user')->where('uid ="'.$uid.'" and diu = ui_id and ui_id = u_id')->select();
        foreach($fanslist as $k => $v){
            if($v['icon'] != ''){
            $iconpath = '/Uploads/person_icon/';
            $iconpath .= substr($v['icon'],0,8).'/';
            $iconpath .= $v['icon'];
            $fanslist[$k]['icon'] = $iconpath;
            }
        }
        $ox = $passage->where('diu ="'.$uid.'"')->count('up_id');
        $oxlist = M()->table('dota2_user_passage,dota2_user_info,dota2_user')->where('diu ="'.$uid.'" and uid = ui_id and ui_id = u_id')->select();
        foreach($oxlist as $k => $v){
            if($v['icon'] != ''){
            $iconpath = '/Uploads/person_icon/';
            $iconpath .= substr($v['icon'],0,8).'/';
            $iconpath .= $v['icon'];
            $oxlist[$k]['icon'] = $iconpath;
            }
        }
        $this->assign('fans',$fans);
        $this->assign('ox',$ox);
        $this->assign('fanslist',$fanslist);
        $this->assign('oxlist',$oxlist);

        //我的留言
        $message = M()->table('dota2_user,dota2_user_info,dota2_message')->where('m_uid = "'.$uid.'" and m_diu = u_id and u_id = ui_id')->select();
        foreach($message as $k => $v){
            $iconpath = '/Uploads/person_icon/';
            $iconpath .= substr($v['icon'],0,8).'/';
            $iconpath .= $v['icon'];
            $message[$k]['icon'] = $iconpath;
        }
        $this->assign('message',$message);

        //我的帖子
        $topic = M('topic')->field('t_id,title,createtime')->where('status=1 and uid='.$uid)->select();
        $this->assign('topic',$topic);

        //收藏的帖子
        $topic_collect = M()->table('dota2_topic t,dota2_topic_collect c')->field('t_id,title,createtime')->where('status=1 and t.t_id=c.tid and c.uid='.$uid)->select();
        // var_dump($topic_collect);
        $this->assign('topic_collect',$topic_collect);

        $this->display();
    }

    public function delbyperson()
    {
        $data['status'] = 0;
        $res = M('topic')->where('t_id='.I('get.t'))->save($data);
        if ($res) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function edit()
    {
        $user = M('user_info');
        if (!$user->create()) {
            echo $user->getError();
        } else {
            echo $user->save();
        }
    }

    public function pwd_edit()
    {
        $res = M('user')->where(['email'=>$_SESSION['email'],'password'=>$_POST['pwd']])->find();
        if (!$res) {
            echo '原密码错误!!';
        }else{
            M('user')->where('email="'.$_SESSION['email'].'"')->setField('password',$_POST['npwd']);
            echo '修改成功';
        }
    }

    public function check_question()
    {
        $data = M('pwdpro')->field('question')->where('uid='.$_GET['uid'])->select()[0]['question'];
        echo $data;
    }

    public function question_set()
    {
        $data['question'] = I('post.question');
        $data['answer'] = I('post.answer');
        $data['uid'] = I('get.uid');
        if (M('pwdpro')->add($data)) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function check_pwdquestion()
    {
        $map['question'] = I('post.question');
        $map['answer'] = I('post.answer');
        $map['uid'] = I('get.uid');
        if (M('pwdpro')->where($map)->find()) {
            echo '1';
        }
    }

    public function question_reset()
    {
        $res = M('pwdpro')->where('uid='.$_GET['uid'])->delete();
        if ($res) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function send_yzm()
    {   
        $send = sendTemplateSMS("18855159920",array($_GET['uid'],1),"1");
    }

    /**
     * 上传头像插件 保存到本地
     */
    public function icon_upload(){

        $imgData = $_POST['src'];
       
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgData, $result)){
            $type = $result[2];
            $date = date('Ymd',time());
            $new_file = "./Uploads/person_icon/".$date."/";
            // echo $new_file;exit;
            if(!file_exists($new_file))
            {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file, 0700);
            }
            $iconname = $date.time().".{$type}";
            $new_file = $new_file.$iconname;
            if (file_put_contents($new_file, base64_decode(str_replace('data:image/png;base64,', '', $imgData)))){
                $id = M('user')->where('email="'.$_SESSION['email'].'"')->find()['u_id'];
                $icon = M('user_info')->where('ui_id='.$id)->find()['icon'];
                if (!empty($icon)) {
                    //删除原头像
                    $filepath = './Uploads/person_icon/';
                    $filepath .= substr($icon,0,8).'/';
                    $scandir = scandir($filepath);
                    $preg = '/('.$icon.'){1}/';
                    $preg = strstr($preg,'.',true).'){1}/';
                    foreach ($scandir as $value) {
                            if (preg_match($preg,$value)) {
                                unlink($filepath.$value);
                            }
                    }
                }
                //更新头像
                M('user_info')->where('ui_id='.$id)->setField('icon',$iconname);
               
                echo '修改成功';
            }else{
                echo '修改失败';
                }
        }
    }

    public function delCollection(){
        $gid = $_POST['gid'];
        $data = M('collection')->where('coll_gid="'.$gid.'"')->delete();
    }

    public function detail(){
        $user = M('user');
        $uid = I('get.uid');
        $data = $user->table('dota2_user_info,dota2_user')->where('ui_id=u_id and u_id="'.$uid.'"')->select();
        $iconpath = '/Uploads/person_icon/';
        $iconpath .= substr($data[0]['icon'],0,8).'/';
        $this->assign('iconpath',$iconpath);
        $this->assign('data',$data[0]);
        $id = $_SESSION['uid'];
        $true = M('user_passage')->where('uid = "'.$uid.'" and diu = "'.$id.'"')->select();
        if($true == null){
            $this->assign('mark','<b>+</b> 关注');
        }else{
            $this->assign('mark','取消关注');
        }
        $this->display();
    }

    //关注与取消关注
    public function concem(){
        $uid = I('post.uid');
        $id = $_SESSION['uid'];
        $false = M('user_passage')->where('uid = "'.$uid.'" and diu = "'.$id.'"')->select();
        if($false == null){
            $model = M('user_passage');
            $data['uid'] = $uid;
            $data['diu'] = $id;
            $model->add($data);
        }else{
            M('user_passage')->where('uid = "'.$uid.'" and diu = "'.$id.'"')->delete();
        }
    }

    //添加留言
    public function addMessage(){
        $content = I('post.val');
        $uid = I('post.uid');
        $id = $_SESSION['uid'];
        $message = M('message');
        $data['m_content'] = $content;
        $data['m_uid'] = $uid;
        $data['m_diu'] = $id;
        $data['m_addtime'] = time();
        $message->add($data);
    }

    //删除留言
    public function delMessage(){
        $mid = I('post.mid');
        M('message')->delete($mid);
    }

    //刀币充值
    public function addCoin(){
        $coin = $_POST['coin'];
        $uid = $_SESSION['uid'];
        $user = M('user_info');
        $rmb = $user->where('ui_id ="'.$uid.'"')->setInc('dotacoin',$coin);
        echo json_encode($rmb);
    }

    //积分兑换
    public function delScore(){
        $score = $_POST['score'];
        $uid = $_SESSION['uid'];
        $user = M('user_info');
        $rmb = $user->where('ui_id ="'.$uid.'"')->setInc('dotacoin',$score);
        $score = $score * 10;
        $score = $user->where('ui_id ="'.$uid.'"')->setDec('score',$score);
        echo json_encode($score);
    }
}
